﻿using System;
using EPiServer.Core;
using EPiServer.Common;
using EPiServer.Web;

namespace EPiServer.Templates.RelatePlus.Templates.PageEntity
{
    public abstract class PageEntity : FrameworkEntityBase, ISiteAssignable
    {
        #region Member Variables
        private readonly string[] _casheKey;
        private PageReference _pageLink = PageReference.EmptyReference;
        #endregion

        #region Constructor
        /// <summary>
        /// Initializes a new instance of the <see cref="PageEntity"/> class.
        /// </summary>
        /// <param name="pageReference">The page reference.</param>
        /// <param name="site">The site.</param>
        public PageEntity(PageReference pageReference, EPiServer.Common.ISite site)
            : this(pageReference, site, PageEntityTypes.Undefined)
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="PageEntity"/> class.
        /// </summary>
        /// <param name="pageReference">The page reference.</param>
        /// <param name="site">The site.</param>
        /// <param name="entityType">Type of the page.</param>
        public PageEntity(PageReference pageReference, EPiServer.Common.ISite site, PageEntityTypes entityType)
            : this(-1, (site != null ? site.ID : -1), PermanentLinkUtility.FindGuid(pageReference), entityType)
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="PageEntity"/> class.
        /// </summary>
        /// <param name="pageGuid">The page GUID.</param>
        /// <param name="site">The site.</param>
        public PageEntity(Guid pageGuid, EPiServer.Common.ISite site)
            : this(pageGuid, site, PageEntityTypes.Undefined)
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="PageEntity"/> class.
        /// </summary>
        /// <param name="pageGuid">The page GUID.</param>
        /// <param name="site">The site.</param>
        /// <param name="entityType">Type of the entity.</param>
        public PageEntity(Guid pageGuid, EPiServer.Common.ISite site, PageEntityTypes entityType)
            : this(-1, (site != null ? site.ID : -1), pageGuid, entityType)
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="PageEntity"/> class.
        /// </summary>
        /// <param name="id">The id.</param>
        /// <param name="siteID">The site ID.</param>
        /// <param name="pageGuid">The page GUID.</param>
        /// <param name="entityType">Type of the entity.</param>
        protected internal PageEntity(int id,int siteID, Guid pageGuid, PageEntityTypes entityType)
            : base(id)
        {
            SiteID = siteID;
            PageGUID = pageGuid;            
            EntityType = entityType;
            _casheKey = ConstructCacheKey(id);                      
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="PageEntity"/> class.
        /// </summary>
        protected internal PageEntity()
            : base(-1)
        {
        }
        #endregion

        #region Properties
        public override string[] CacheKey
        {
            get { return _casheKey; }
        }

        /// <summary>
        /// Gets the page link.
        /// </summary>
        /// <value>The page link.</value>
        public PageReference PageLink 
        {
            get
            {
                if (PageReference.IsNullOrEmpty(_pageLink))
                {
                    _pageLink = PermanentLinkUtility.FindPageReference(PageGUID);
                }
                return _pageLink;
            }
        }
        public Guid PageGUID { get; private set; }
        public PageEntityTypes EntityType { get; set; }
        public int SiteID { get; set; }
        #endregion

        #region Internal Methods
        internal static string[] ConstructCacheKey(int id)
        {
            return new [] { "PageEntity", "PageEntity", id.ToString() };
        }

        internal static string[] ConstructCacheKey(Guid guid)
        {
            return new [] { "PageEntity", "PageEntity", guid.ToString() };
        }
        #endregion

        #region ISiteAssignable Members

        public ISite Site
        {
            get
            {
                return SiteHandler.GetSite(SiteID);
            }
        }

        #endregion
    }
}